package fun.linze.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import fun.linze.domain.TUser;
import fun.linze.dto.UserDto;
import fun.linze.dto.UserRegisterDto;
import fun.linze.service.EmailService;
import fun.linze.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    private final TUserService userService;
    private final EmailService emailService;

    @Autowired
    public UserController(TUserService userService, EmailService emailService) {
        this.userService = userService;
        this.emailService = emailService;
    }

    // POST /api/v1/users - (用户注册)
    @PostMapping(consumes = "application/json")
    public ResponseEntity<?> registerUser(@RequestBody UserRegisterDto userRegisterDto) {
        if (!emailService.verifyCode(userRegisterDto.getEmail(), userRegisterDto.getCode())) {
            return ResponseEntity.status(400).body("Invalid verification code");
        }
        try {
            userService.registerUser(userRegisterDto);
            return ResponseEntity.ok().build();
        } catch (IllegalStateException e) {
            return ResponseEntity.status(409).body(e.getMessage());
        }
    }

    // GET /api/v1/users/{id} - (获取指定ID用户的公开信息)
    @GetMapping("/{id}")
    public ResponseEntity<UserDto> getUserById(@PathVariable Long id) {
        TUser user = userService.getById(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setUsername(user.getUsername());
        userDto.setNickname(user.getNickname());
        return ResponseEntity.ok(userDto);
    }

    // GET /api/v1/users/me - (获取当前登录用户的信息)
    @GetMapping("/me")
    public ResponseEntity<UserDto> getCurrentUser() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        TUser user = userService.getOne(new QueryWrapper<TUser>().eq("username", username));
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setUsername(user.getUsername());
        userDto.setNickname(user.getNickname());
        return ResponseEntity.ok(userDto);
    }

    // PATCH /api/v1/users/me - (更新当前登录用户的信息)
    @PatchMapping("/me")
    public ResponseEntity<UserDto> updateCurrentUser(@RequestBody UserDto userDto) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        TUser user = userService.getOne(new QueryWrapper<TUser>().eq("username", username));
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        user.setNickname(userDto.getNickname());
        userService.updateById(user);

        UserDto updatedUserDto = new UserDto();
        updatedUserDto.setId(user.getId());
        updatedUserDto.setUsername(user.getUsername());
        updatedUserDto.setNickname(user.getNickname());
        return ResponseEntity.ok(updatedUserDto);
    }
}
